package com.example.recyclerviewdemo2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.RelativeLayout;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
    }


    private void intiView(){
        SwipeRecyclerView con=findViewById(R.id.contact_list);
        SmartRefreshLayout refreshLayout=findViewById(R.id.smart_refresh);

        con.setLayoutManager(new LinearLayoutManager(this));
        List<ContactModel> list=new ArrayList<>();
        List<ContactModel> newList=new ArrayList<>();
        for (int i=0;i<20;i++){
            ContactModel model =new ContactModel();
            model.setContactName("张三"+i);
            model.setContactIconResId(R.mipmap.ic_launcher);
            list.add(model);

            ContactModel newModel =new ContactModel();
            newModel.setContactName("李四"+i);
            newModel.setContactIconResId(R.mipmap.ic_launcher);
            newList.add(newModel);
       }
        MyAdapter adapter=new MyAdapter(this);
        adapter.setList(list);


        SwipeMenuCreator menuCreator=new SwipeMenuCreator() {
            //参数1   左侧菜单
            //参数2   右侧菜单
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem deleteItem=new SwipeMenuItem(MainActivity.this);
                deleteItem.setBackground(R.color.design_default_color_error);
                deleteItem.setText("删除");
                deleteItem.setTextColor(Color.WHITE);
                deleteItem.setTextSize(16);
                //菜单高度设置为与条目高度相同，所以通常使用ViewGroup.LayoutParams.MATCH_PARENT
                deleteItem.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
                deleteItem.setWidth(160);
                //将菜单项添加到右侧菜单中
                rightMenu.addMenuItem(deleteItem);

            }
        };
        con.setSwipeMenuCreator(menuCreator);
        //设置菜单点击事件
        con.setOnItemMenuClickListener(new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, int adapterPosition) {
                //判断点击是否为右侧菜单
                if(menuBridge.getPosition()==SwipeRecyclerView.RIGHT_DIRECTION){
                    //根据SwipeMenuCreator创建时添加的顺序判断具体某个菜单，
                    //先添加的序号为0，之后以此类推，类似数组脚标
                    if(menuBridge.getPosition()==0){
                        //点击时收起菜单
                        menuBridge.closeMenu();
                        //单条数据的局部刷新 使用RecyclerView.Adapter.notifyItemxxx()
                        //通知列表条目被移除  adapterPosition列表条目脚标
                        adapter.notifyItemRemoved(adapterPosition);
                        //移除数据list中的指定元素
                        list.remove(adapterPosition);
                        adapter.notifyItemRangeRemoved(adapterPosition,list.size()-1);
                    }
                }
            }
        });
        con.setAdapter(adapter);
        //延迟自动刷新  单位ms
        //refreshLayout.autoRefresh(200);
        //延迟自动加载
        //refreshLayout.autoLoadMore(200);
        //如果设置了刷新加载事件，则默认的监听收起延迟时长会消失
        //需要手动结束刷新、加载
        //单独监听刷新事件
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //数据和适配器之间一一匹配，即一套list数据对应一个adapter适配器
                //无论如何书信，数据List对象并没有发生变化，仅修改数据
                list.clear();
                list.addAll(newList);
                //无脑刷 刷新全部数据，整个RecyclerView重绘
                //当数据刷新完成后，必须通知适配器，否则适配器不会重绘复合控件
                adapter.notifyDataSetChanged();
                refreshLayout.finishRefresh(3000,true,false);

            }
        });
        //单独监听加载事件
        //移动分页使用的是增量加载，新数据会累加在老数据之上
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull  RefreshLayout refreshLayout) {
                //累加新数据
                list.addAll(newList);
                adapter.notifyDataSetChanged();
                refreshLayout.finishLoadMore(3000,true,false);
            }
        });
        //同时监听加载/刷新事件
        //单独监听和同时监听不可同时使用
        /*refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull  RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }
        });
*/
    }
}